/**
 *
 */
import puppeteer from "puppeteer";

const getState = (data: string[]) => {
  let state = "";

  // 是否易挥发  -1:未知 0:否 1:是
  let isVolatile = -1;

  // 是否粘稠 -1:未知 0:否 1:是
  let isViscose = -1;

  if (data.length) {
    data.forEach((item) => {
      if (item) {
        if (item.indexOf("液体") >= 0) {
          state = "liquid";

          if (
            item.indexOf("粘性") >= 0 ||
            item.indexOf("油状") >= 0 ||
            item.indexOf("粘稠") >= 0
          ) {
            isViscose = 1;
          }
        } else if (item.indexOf("粉末") >= 0 || item.indexOf("晶体") >= 0) {
          state = "powder";
        }

        if (item.indexOf("挥发") >= 0) {
          isVolatile = 1;
        }
      }
      return false;
    });
  }

  return {
    state,
    isVolatile,
    isViscose,
  };
};

const searchReagentByChemsrcCas = async (casno: string) => {

  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto(`https://www.chemsrc.com/searchResult/${casno}/`);
  const TABLE_SELECTOR = ".col-md-9";
  const brands: any = await page.evaluate((sel) => {
    const trlist = Array.from($(sel).find("#baseTbl tr th"));
    let infoArr = [];
    trlist.forEach((v: any) => {
      let label = v.innerText.replace(/\s/g, "");
      let value = v.nextElementSibling.innerText.replace(/\s/g, "");
      if (label === "常用名") {
        infoArr.push({ label: "cnname", value });
      } else if (label === "英文名") {
        infoArr.push({ label: "enname", value });
      } else if (label === "分子量") {
        infoArr.push({ label: "mw", value });
      } else if (label === "密度") {
        infoArr.push({ label: "density", value });
      } else if (label === "分子式") {
        infoArr.push({ label: "formula", value });
      } else if (label === "熔点") {
        infoArr.push({ label: "mp", value });
      } else if (label === "沸点") {
        infoArr.push({ label: "bp", value });
      }
    });
    let descs: string[] = [];
    const desclist = Array.from($(sel).find("#xingTbl tr th"));
    desclist.forEach((v: any) => {
      let label = v.innerText.replace(/\s/g, "");
      let value = v.nextElementSibling.innerText.replace(/\s/g, "");
      if (label === "外观性状") {
        descs.push(value);
      } else if (label === "更多") {
        let tempValue = v.nextElementSibling.children[0].innerText;
        descs.push(tempValue);
      }
    });
    const desc2list = Array.from($(sel).find("#msdsControl b"));
    desc2list.forEach((v: any) => {
      let label = v.innerText.replace(/\s/g, "");
      if (label.indexOf("基本的理化特性的信息") >= 0) {
        let nextNode = v;
        let index = 0;
        while (index < 7) {
          nextNode = nextNode.nextSibling;
          let nodetype = nextNode.nodeType;
          let nodevalue = nextNode.nodeValue;
          if (nodetype === 3 && nodevalue && nodevalue.trim()) {
            let nodevaluetrim = nodevalue.trim();
            if (nodevaluetrim !== "a) 外观与性状") {
              descs.push(nodevaluetrim);
            }
          }
          index++;
        }
      }
    });
    infoArr.push({ label: "desc", value: descs });
    return infoArr;
  }, TABLE_SELECTOR);
  await browser.close();
  let stateInfo: any = null;
  if (brands && brands.length) {
    let tempObj: any = {};
    let tempState = {};
    brands.forEach((item: any, index: number) => {
      const { label, value } = item;
      if (label === "desc") {
        tempState = getState(value);
      } else {
        tempObj[label] = value;
      }
    });

    if (tempObj && !tempObj.cnname && !tempObj.enname) {
      stateInfo = null;
    } else {
      stateInfo = {
        ...tempObj,
        ...tempState,
      };
    }
  }
  return stateInfo;


};

export default searchReagentByChemsrcCas;
